######################################
##########CONJOINT ANALYSIS###########
######################################

#INSTALL CREGG
if (!require("remotes")) {
  install.packages("remotes")
}
remotes::install_github("leeper/cregg")

#LOAD /INSTALL PACKAGES
pacman::p_load(
  cregg, foreign, haven, sjmisc, magrittr, tidyverse, ggpubr, 
  patchwork
)

### SET WORKING DIRECTORY TO FILE LOCATION
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

#LOAD DATA
paper_risp<-read.dta("goodpolitician.dta")

#SET GRAPH LABELS
labamce <- c("strong leader, hinders collegiality", "no strong leader, favour collegiality", "(LEADERSHIP)", 
            "focused on the interests of her/his voters", "focused on the wide public", "(VISION OF ROLE)", 
            "no policy/no english", "policy/no english", "no policy/english", "policy/english", "(COMPETENCE)",
            "under investigation", "clean record", "(INTEGRITY)",
            "distant", "caring", "(SOCIAL SKILLS)",
            "coarse and rude", "proper and refined", "(COMMUNICATION)",
            "politician", "manual worker", "farmer", "manager", "uni professor", "engineer", "(JOB POSITION)",
            "man", "woman", "(GENDER)")

labmm <- c("strong leader, hinder collegiality", "no strong leader, favour collegiality", "(LEADERSHIP)", 
             "focused on the interests of her/his voters", "focused on the wide public", "(VISION OF ROLE)", 
             "no policy/no english", "policy/no english", "no policy/english", "policy/english", "(COMPETENCE)",
             "under investigation", "clean record", "(INTEGRITY)",
             "distant", "caring", "(SOCIAL SKILLS)",
             "coarse and rude", "proper and refined", "(COMMUNICATION)",
             "politician", "manual worker", "farmer", "manager", "uni professor", "engineer", "(JOB POSITION)",
             "man", "woman", "(GENDER)")

#SET REFERENCE CATEGORY
paper_risp$SESSO <- relevel(paper_risp$SESSO, ref=1)
paper_risp$POS<- relevel(paper_risp$POS, ref=6)
paper_risp$STILE <- relevel(paper_risp$STILE, ref=2)
paper_risp$CAPA <- relevel(paper_risp$CAPA, ref=2)
paper_risp$INTE <- relevel(paper_risp$INTE, ref=2)
paper_risp$COMPE <- relevel(paper_risp$COMPE, ref=1)
paper_risp$VISI <- relevel(paper_risp$VISI, ref=2)
paper_risp$LEAD <- relevel(paper_risp$LEAD, ref=1)

#AMCE
amces_gp <- cj(paper_risp, CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD, 
               weights = ~ weight1_istr_w2, id = ~id)

amces_gp_graph <- plot(amces_gp) + ggplot2::theme(legend.position = "none") + scale_y_discrete(labels= labamce) +
  xlab("Change in the Pr of winning a seat in EUP (AMCE)") +
  geom_point(size = 4) +
  theme(axis.text=element_text(size=16),
        axis.title=element_text(size=16,face="bold")) +
  theme(strip.text = element_text(size=16))

ggsave("figure6.png", dpi = 600)


#POPULISM (MEDIAN)
paper_risp$popmedian2  <- factor(paper_risp$popmedian2 , levels=c("0", "1"), labels=c("Low populism", "High populism"))

#AMCE vs DIFF
amce_by_populism <- cj(
  subset(paper_risp, !is.na(popmedian2)),
  CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD,
  weights = ~ weight1_istr_w2, id = ~ id,
  estimate = "amce",
  by = ~ popmedian2
)

amce_diff_populism <- cj(
  subset(paper_risp, !is.na(popmedian2)),
  CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD,
  weights = ~ weight1_istr_w2, id = ~ id,
  estimate = "amce_diff",
  by = ~ popmedian2
)

plot(rbind(amce_by_populism, amce_diff_populism)) + ggplot2::facet_wrap(~BY, ncol = 3L) +
  ggplot2::theme(legend.position = "none") + scale_y_discrete(labels= labamce) +
  geom_point(size = 4) +
  theme(axis.text=element_text(size=16),
        axis.title=element_text(size=16,face="bold")) +
  theme(strip.text = element_text(size=18))

ggsave("figure7.png", dpi = 600)


#TEST FOR PREFERENCE HETEORGENEITY
paper_risp_anova<-read.dta("goodpolitician_anovamedian.dta")

cj_anova(paper_risp_anova,  CHO ~ SESSO, by = ~popmedian2)
cj_anova(paper_risp_anova,  CHO ~ POS, by = ~popmedian2)
cj_anova(paper_risp_anova,  CHO ~ STILE, by = ~popmedian2)
cj_anova(paper_risp_anova,  CHO ~ CAPA, by = ~popmedian2)
cj_anova(paper_risp_anova,  CHO ~ INTE, by = ~popmedian2)
cj_anova(paper_risp_anova,  CHO ~ COMPE, by = ~popmedian2)
cj_anova(paper_risp_anova,  CHO ~ VISI, by = ~popmedian2)
cj_anova(paper_risp_anova,  CHO ~ LEAD, by = ~popmedian2)

cj_anova(paper_risp_anova,  CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD, by = ~popmedian2)

